﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Text;
using System.Text.RegularExpressions;

public partial class Manage_Admin_PlusFlag : System.Web.UI.Page
{
    private ManageErrMsg nsb = new ManageErrMsg();
    private KeRui.BLL.Admin bllAdmin = new KeRui.BLL.Admin();
    protected KeRui.Model.Admin modelAdmin;
    protected KeRui.BLL.AdminFlag bllAdminFlag = new KeRui.BLL.AdminFlag();
    protected int userID = -1;

    protected int FlagType = PageRequest.GetInt("flagType",0);
    protected string content;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        ManageHelper.CheckAdminLogin();
        userID = PageRequest.GetInt("userID",0);
        modelAdmin = bllAdmin.GetModel(userID);
        if (modelAdmin == null)
        {
            nsb.AddErr("Error：不存在userid"); nsb.ChkErr();
        }
        switch (ManageHelper.PageAct())
        {
            case "save":
                Save();
                break;
            default:
                break;
        }
        
        content = GetRoleFlagList(FlagType, modelAdmin.RoleFlags, modelAdmin.PlusFlag); 
        
    }

    #region 添加
    private void Save()
    {

        KeRui.Model.Admin modelAdmin_old = new KeRui.Model.Admin();
        modelAdmin_old = bllAdmin.GetModel(userID);
        string pattern = @"site_\w*_\d+";
        Regex regex = new Regex(pattern);
        MatchCollection collection = regex.Matches(modelAdmin_old.PlusFlag);
        string siteRoleFlag="";
        foreach (Match match in collection)
        {
            //把站点权限全部找出来
            siteRoleFlag += match.Captures[0].ToString()+",";
        }

        //如果为附加权限则把所有的系统权限全部找出来_ZJY
        if (FlagType >= 5)
        {
            Regex reg = new Regex(@"system_\w*");
            MatchCollection mcollection = reg.Matches(modelAdmin_old.PlusFlag);
            foreach (Match m in mcollection)
            {
                siteRoleFlag += m.Groups[0].ToString() + ",";
            }
        }

        modelAdmin.PlusFlag = siteRoleFlag+PageRequest.GetString("RoleFlag");
        #region modelAdmin.UserFlag
        modelAdmin.UserFlag = "";
        if (modelAdmin.PlusFlag == "")
        {
            if (modelAdmin.RoleFlags != "")
            {
                modelAdmin.UserFlag = modelAdmin_old.RoleFlags;
            }
        }
        else
        {
            if (modelAdmin.RoleFlags == "")
            {
                modelAdmin.UserFlag = modelAdmin.PlusFlag;
            }
            else
            {
                modelAdmin.UserFlag = modelAdmin_old.RoleFlags + "," + modelAdmin.PlusFlag;
            }
        }
        #endregion
        bllAdmin.UpdateForPlusSysRoleFlag(modelAdmin);
        Response.Redirect("PlusFlag.aspx?flagType="+FlagType+"&userID=" + userID + "&sucmsg=保存成功。");
    }
    #endregion

    #region 权限列表
    int EmptyNum = 0;
    protected string GetRoleFlagList(int FlagType,string RoleFlag, string PlusFlag)
    {
        RoleFlag = "," + RoleFlag + ",";
        PlusFlag = "," + PlusFlag + ",";
        StringBuilder xs = new StringBuilder();
        DataTable dt = bllAdminFlag.GetRoleChildFlag(0,FlagType).Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string checkflag = "";
            if (RoleFlag.IndexOf("," + dt.Rows[i][4].ToString() + ",") != -1)
            {
                checkflag = "checked";
            }
            else
            {
                if (PlusFlag.IndexOf("," + dt.Rows[i][4].ToString() + ",") != -1)
                {
                    checkflag = "checked";
                }
            }
            EmptyNum = 0;
            xs.AppendFormat("<div id=\"p{0}\"><ul class=\"ultitle\" id=\"t{0}\"><input type=\"checkbox\" name=\"RoleFlag\" id=\"{0}\" " + checkflag + "  value=\"{3}\" onclick=\"selAll(this.id)\">" +
                "<img align=\"absmiddle\" src=\"/Manage/pic/menu/{2}\"/>&nbsp;<span id=\"s{0}\"onclick=\"showchildnode(this.id)\" style=\"cursor:pointer;\">{1}</span></ul>",
                dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString());
            xs.Append(GetChildRoleFlagList(Convert.ToInt32(dt.Rows[i][0]), RoleFlag, PlusFlag));
            xs.Append("</div>");
        }
        return xs.ToString();
    }
    private string GetChildRoleFlagList(int pID, string RoleFlag, string PlusFlag)
    {
        string xs = "";
        for (int n = 0; n < EmptyNum; n++)
        {
            xs += "　";
        }
        StringBuilder strRFlaglist = new StringBuilder();
        if (EmptyNum == 0)
        {
            strRFlaglist.Append("<ul class=\"divurl\" id=\"c" + pID + "\" style=\"border:1px solid #99CCFF;\">");
        }
        else
        {
            strRFlaglist.Append("<ul class=\"divurl\" id=\"c" + pID + "\">");
        }
        DataTable dtChild = bllAdminFlag.GetRoleChildFlag(pID,FlagType).Tables[0];
        for (int m = 0; m < dtChild.Rows.Count; m++)
        {
            string checkflag = "";
            if (RoleFlag.IndexOf("," + dtChild.Rows[m][4].ToString() + ",") != -1)
            {
                checkflag = "checked";
            }
            else
            {
                if (PlusFlag.IndexOf("," + dtChild.Rows[m][4].ToString() + ",") != -1)
                {
                    checkflag = "checked ";
                }
            }
            strRFlaglist.AppendFormat("<li>" + xs + "<input type=\"checkbox\"  name=\"RoleFlag\" id=\"{0}\" value=\"{3}\" " + checkflag + " onclick=\"conclick(this.id," + pID + ")\">" +
                "<img align=\"absmiddle\" src=\"/Manage/pic/menu/{2}\"/>&nbsp;{1}</li>", dtChild.Rows[m][0].ToString(), dtChild.Rows[m][1].ToString(),
                dtChild.Rows[m][3].ToString().ToLower() == "" ? "item.gif" : dtChild.Rows[m][3].ToString(), dtChild.Rows[m][4].ToString());
            if (dtChild.Rows[m]["HaveChildNav"].ToString().ToLower() == "true")
            {
                EmptyNum++;
                strRFlaglist.Append(GetChildRoleFlagList(Convert.ToInt32(dtChild.Rows[m][0]), RoleFlag, PlusFlag));
            }
        }
        strRFlaglist.Append("</ul>");
        return strRFlaglist.ToString();
    }
    #endregion
    
}
